import os
import time
from datetime import datetime, timedelta
from Monitor.feishu import FeishuTalk
from config import start_url, domain


def count_files_and_size(directory):
    total_files = 0
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(directory):
        for filename in filenames:
            file_path = os.path.join(dirpath, filename)
            if os.path.isfile(file_path):
                total_files += 1
                total_size += os.path.getsize(file_path)
    return total_files, total_size


def main(current_directory):
    end_time = datetime.now() + timedelta(days=3)  # 设置结束时间为三天后

    while datetime.now() < end_time:
        total_files, total_size = count_files_and_size(current_directory)
        t1 = f"当前时间: {datetime.now()}"
        t2 = f"文件数量: {total_files}"
        t3 = f"文件夹大小: {total_size / (1024 ** 2):.2f} MB"
        content = "\n".join([start_url, domain, t1, t2, t3])
        FeishuTalk().sendTextmessage(content)
        time.sleep(180)  # 等待3分钟


if __name__ == "__main__":
    current_directory = 'json/' + domain  # 当前文件夹
    main(current_directory)
